<?php
/**
 * RcPage class file
 *
 * @author         RcPHP Dev Team
 * @copyright      Copyright (c) 2013,RcPHP Dev Team
 * @license        Apache License 2.0 {@link http://www.apache.org/licenses/LICENSE-2.0}
 * @package        core
 * @since          1.0
 * @filesource
 */
defined('IN_RCPHP') or exit('Access denied');

class RcPage extends RcBase
{

	/**
	 * 连接网址
	 * @var string
	 */
	public $url;

	/**
	 * 当前页
	 * @var integer
	 */
	public $page;

	/**
	 * list总数
	 *
	 * @var integer
	 */
	public $total;

	/**
	 * 分页总数
	 * @var integer
	 */
	public $totalPages;

	/**
	 * 每个页面显示的post数目
	 *
	 * @var integer
	 */
	public $num;

	/**
	 * list允许放页码数量,如:1.2.3.4就这4个数字,则$perCircle为4
	 *
	 * @var integer
	 */
	public $perCircle;

	/**
	 * 分页程序的扩展功能开关,默认关闭
	 * @var boolean
	 */
	public $ext;

	/**
	 * list中的坐标. 如:7,8,九,10,11这里的九为当前页,在list中排第三位,则$center为3
	 *
	 * @var integer
	 */
	public $center;

	/**
	 * 第一页
	 * @var string
	 */
	public $firstPage;

	/**
	 * 上一页
	 * @var string
	 */
	public $prePage;

	/**
	 * 下一页
	 * @var string
	 */
	public $nextPage;

	/**
	 * 最后一页
	 * @var string
	 */
	public $lastPage;

	/**
	 * 分页附属说明
	 * @var string
	 */
	public $note;

	/**
	 * 是否为ajax分页模式
	 *
	 * @var boolean
	 */
	public $isAjax;

	/**
	 * ajax分页的动作名称
	 *
	 * @var string
	 */
	public $ajaxActionName;

	/**
	 * 分页隐藏开关
	 * @var boolean
	 */
	public $hiddenStatus;

	/**
	 * 构造函数
	 * @access public
	 * @return boolean
	 */
	public function __construct()
	{

		$this->ext = false;
		$this->center = 3;
		$this->num = 10;
		$this->perCircle = 10;
		$this->isAjax = false;
		$this->hiddenStatus = false;

		return true;
	}

	/**
	 * 获取总页数
	 * @return integer
	 */
	private function getTotalPage()
	{
		if(empty($this->total))
		{
			return false;
		}

		return ceil($this->total / $this->num);
	}

	/**
	 * 获取当前页数
	 * @return integer
	 */
	private function getPageNum()
	{
		$page = (!$this->page) ? 1 : (int)$this->page;

		return ($page > $this->totalPages) ? (int)$this->totalPages : $page;
	}

	/**
	 * 返回$this->num=$num.
	 *
	 * @param integer $num
	 * @return $this
	 */
	public function num($num = null)
	{
		if(is_null($num))
		{
			$num = 20;
		}

		$this->num = intval($num);

		return $this;
	}

	/**
	 * 返回$this->total=$totalPost.
	 *
	 * @param integer $totalPost
	 * @return $this
	 */
	public function total($totalPost = null)
	{
		$this->total = (!is_null($totalPost)) ? (int)$totalPost : 0;

		return $this;
	}

	/**
	 * 返回$this->url=$url.
	 *
	 * @param string $url
	 * @return $this
	 */
	public function url($url = null)
	{
		if(is_null($url))
		{

			//当网址没有参数时
			$url = (!$_SERVER['QUERY_STRING']) ? $_SERVER['REQUEST_URI'] . ((substr($_SERVER['REQUEST_URI'], -1) == '?') ? 'page=' : '?page=') : '';

			//当网址有参数时,且有分页参数(page)时
			if(!$url && (stristr($_SERVER['QUERY_STRING'], 'page=')))
			{
				$url = str_ireplace('page=' . $this->page, '', $_SERVER['REQUEST_URI']);

				$end_str = substr($url, -1);
				if($end_str == '?' || $end_str == '&')
				{
					$url .= 'page=';
				}
				else
				{
					$url .= '&page=';
				}
			}

			//当网址中未发现含有分页参数(page)时
			if(!$url)
			{
				$url = $_SERVER['REQUEST_URI'] . '&page=';
			}
		}

		if(empty($url))
		{
			RcController::halt('The argument of method : url() invalid in pager class!');
		}

		$this->url = trim($url);

		return $this;
	}

	/**
	 * 返回$this->page=$page.
	 *
	 * @param integer $page
	 * @return $this
	 */
	public function page($page = null)
	{
		if(is_null($page))
		{
			$page = RcRequest::get('page') !== false ? intval(RcRequest::get('page', true)) : 1;
		}

		if(empty($page))
		{
			RcController::halt('The argument of method : page() invalid in pager class!');
		}

		$this->page = $page;

		return $this;
	}

	/**
	 * 返回$this->ext=$ext.
	 *
	 * @param boolean $ext
	 * @return $this
	 */
	public function ext($ext = true)
	{
		//将$ext转化为小写字母.
		$this->ext = ($ext) ? true : false;

		return $this;
	}

	/**
	 * 返回$this->center=$num.
	 *
	 * @param integer $num
	 * @return $this
	 */
	public function center($num)
	{
		if(empty($num))
		{
			return false;
		}

		$this->center = (int)$num;

		return $this;
	}

	/**
	 * 返回$this->perCircle=$num.
	 *
	 * @param integer $num
	 * @return $this
	 */
	public function circle($num)
	{
		if(empty($num))
		{
			return false;
		}

		$this->perCircle = intval($num);

		return $this;
	}

	/**
	 * 处理上一页
	 * @return string
	 */
	private function getPrevPage()
	{
		if($this->isAjax === true)
		{
			$string = '<li><a ' . (($this->page == 1 || $this->totalPages <= 1) ? 'href="#"' : 'href="javascript:;" onclick="' . $this->ajaxActionName . '(\'' . $this->url . ($this->page - 1) . '\'); return false;"') . '>上一页</a></li>';
		}
		else
		{
			$string = '<li><a ' . (($this->page == 1 || $this->totalPages <= 1) ? 'href="#"' : 'href="' . $this->url . ($this->page - 1) . '" target="_self"') . '>上一页</a></li>';
		}

		return $string;
	}

	/**
	 * 处理下一页
	 * @return string
	 */
	private function getNextPage()
	{
		if($this->isAjax === true)
		{
			$string = '<li><a ' . (($this->page == $this->totalPages || $this->totalPages <= 1) ? 'href="#"' : 'href="javascript:;" onclick="' . $this->ajaxActionName . '(\'' . $this->url . ($this->page + 1) . '\'); return false;"') . '>下一页</a></li>';
		}
		else
		{
			$string = '<li><a ' . (($this->page == $this->totalPages || $this->totalPages <= 1) ? 'href="#"' : 'href="' . $this->url . ($this->page + 1) . '" target="_self"') . '>下一页</a></li>';
		}

		return $string;
	}

	/**
	 * 处理第一页,上一页
	 * @return string
	 */
	private function getFirstPage()
	{
		if($this->isAjax === true)
		{
			$string = '<li><a ' . (($this->page == 1 || $this->totalPages <= 1) ? 'href="#"' : 'href="javascript:;" onclick="' . $this->ajaxActionName . '(\'' . $this->url . '1\'); return false;"') . '>首页</a></li>';
		}
		else
		{
			$string = '<li><a ' . (($this->page == 1 || $this->totalPages <= 1) ? 'href="#"' : 'href="' . $this->url . '1" target="_self"') . '>首页</a></li>';
		}

		return $string;
	}

	/**
	 * 处理下一页,最后一页
	 * @return string
	 */
	private function getLastPage()
	{
		if($this->isAjax === true)
		{
			$string = '<li><a ' . (($this->page == $this->totalPages || $this->totalPages <= 1) ? 'href="#"' : 'href="javascript:;" onclick="' . $this->ajaxActionName . '(\'' . $this->url . $this->totalPages . '\'); return false;"') . '>最后一页</a></li>';
		}
		else
		{
			$string = '<li><a ' . (($this->page == $this->totalPages || $this->totalPages <= 1) ? 'href="#"' : 'href="' . $this->url . $this->totalPages . '" target="_self"') . '>最后一页</a></li>';
		}

		return $string;
	}

	/**
	 * 处理list内容
	 *
	 * @return string
	 */
	private function getList()
	{
		if(empty($this->totalPages) || empty($this->page))
		{
			return false;
		}

		if($this->totalPages > $this->perCircle)
		{
			if($this->page + $this->perCircle >= $this->totalPages + $this->center)
			{
				$list_start = $this->totalPages - $this->perCircle + 1;
				$list_end = $this->totalPages;
			}
			else
			{
				$list_start = ($this->page > $this->center) ? $this->page - $this->center + 1 : 1;
				$list_end = ($this->page > $this->center) ? $this->page + $this->perCircle - $this->center : $this->perCircle;
			}
		}
		else
		{
			$list_start = 1;
			$list_end = $this->totalPages;
		}

		$pageListQueue = '';
		for($i = $list_start; $i <= $list_end; $i++)
		{
			$pageListQueue .= ($this->page == $i) ? '<li class="fy_blue"><a href="#">' . $i . '</a></li>' : (($this->isAjax === true) ? '<li><a onclick="' . $this->ajaxActionName . '(\'' . $this->url . $i . '\'); return false;">' . $i . '</a></li>' : '<li><a href="' . $this->url . $i . '" target="_self">' . $i . '</a></li>');
		}

		return $pageListQueue;
	}

	/**
	 * 开启ajax分页模式
	 *
	 * @param string $action
	 * @return $this
	 */
	public function ajax($action)
	{
		if($action)
		{
			$this->isAjax = true;
			$this->ajaxActionName = $action;
		}

		return $this;
	}

	/**
	 * 輸出處理完成的HTML代碼
	 *
	 * @param int $first
	 * @param int $last
	 * @param int $prev
	 * @param int $next
	 * @param int $total
	 * @param int $input
	 * @return string
	 */
	public function output($first = 1, $last = 1, $prev = 1, $next = 1,  $total= 1,  $input= 0)
	{
		//支持长的url.
		$this->url = trim(str_replace(array(
										   "\n",
										   "\r"
									  ), '', $this->url));

		//获取总页数.
		$this->totalPages = $this->getTotalPage();

		//获取当前页.
		$this->page = $this->getPageNum();
		if($this->total <= $this->num)
		{
			return '';
		}
		else
		{
			$string = '<ul>';
			$string .= $first === 1 ? $this->getFirstPage() : '';
			$string .= $prev === 1 ? $this->getPrevPage() : '';
			$string .= $this->getList();
			$string .= $next === 1 ? $this->getNextPage() : '';
			$string .= $last === 1 ? $this->getLastPage() : '';
			$string .= $total === 1 ? '<li>共 ' . $this->totalPages . ' 页</li>' : '';
			$string .= $input === 1 ? $this->input() : '';
			$string .= '</ul>';

			return $string;
		}
	}

	/**
	 * 输出文本框的页面跳转
	 * @return string
	 */
	public function input()
	{
		//支持长的url.
		$this->url = trim(str_replace(array(
										   "\n",
										   "\r"
									  ), '', $this->url));

		//获取总页数.
		$this->totalPages = $this->getTotalPage();

		//获取当前页.
		$this->page = $this->getPageNum();

		$string = '<li class="goto">跳转至<input type="text" value="' . $this->page . '" id="page_go" name="p" class="gonum">页&nbsp;<a onclick="self.location.href=\'' . $this->url . '\' + document.getElementById(\'page_go\').value" href="javascript:void(0);" class="btn_go">确定</a></li>';

		return $string;
	}
}